package com.ybybh.gamecenter.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Random;

@Controller
public class ZtzGameController {
    //声明一个代表结果消息的属性
    private static String resultMessage = "";
    //声明一个代表随机工具类
    private static Random random = new Random();
    //声明一个代表玩家
    private static int wjsz = 0;
    //声明一个代表电脑
    private static int zjsz = 0;
    //掷骰子游戏真正的游玩的控制器方法
    @RequestMapping("/ztzplay")
    public ModelAndView toZtzplay(){
        wjsz = random.nextInt(6)+1; //随机产生一个1-6的随机数，代表玩家掷骰子的结果
        zjsz = random.nextInt(6)+1; //随机产生一个1-6的随机数，代表电脑掷骰子的结果
        if(wjsz>zjsz){
            resultMessage = "恭喜您，庄家掷出的骰子为"+zjsz+"，您赢了！<a href='/ztz'>再来一局</a>";
        }else if(wjsz<zjsz){
            resultMessage = "很遗憾，庄家掷出的骰子为"+zjsz+"，您输了！<a href='/ztz'>再来一局</a>";
        }else {
            resultMessage = "庄家掷出的骰子为"+zjsz+"，平局了！<a href='/ztz'>再来一局</a>";
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("wjsz", wjsz);
        modelAndView.addObject("resultMessage", resultMessage);
        modelAndView.setViewName("ztzgame");
        return modelAndView;
    }
    //进入到掷骰子界面
    @RequestMapping("/ztz")
    public ModelAndView toZtzHtml(){
        wjsz  = 0;
        zjsz  = 0;
        resultMessage = "请开始游戏！";
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resultMessage", resultMessage);
        modelAndView.setViewName("ztzgame");
        return modelAndView;
    }
}
